<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="需求标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入需求标题" />
      </el-form-item>
      <el-form-item label="物资类别" prop="categoryId">
        <el-select v-model="form.categoryId" placeholder="请选择物资类别">
          <el-option
            v-for="item in categoryOptions"
            :key="item.categoryId"
            :label="item.categoryName"
            :value="item.categoryId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="需求数量" prop="quantity">
        <el-input-number v-model="form.quantity" :min="1" />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input v-model="form.unit" placeholder="请输入单位" />
      </el-form-item>
      <el-form-item label="期望收到日期" prop="expectedDate">
        <el-date-picker
          v-model="form.expectedDate"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="需求描述" prop="description">
        <el-input
          v-model="form.description"
          type="textarea"
          placeholder="请输入需求描述"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getDemand, updateDemand } from '@/api/aid/demand'
import { listCategory } from '@/api/aid/category'

export default {
  name: 'EditDemand',
  data() {
    return {
      form: {
        demandId: undefined,
        title: undefined,
        categoryId: undefined,
        quantity: 1,
        unit: undefined,
        expectedDate: undefined,
        description: undefined
      },
      categoryOptions: [],
      rules: {
        title: [
          { required: true, message: '请输入需求标题', trigger: 'blur' }
        ],
        categoryId: [
          { required: true, message: '请选择物资类别', trigger: 'change' }
        ],
        quantity: [
          { required: true, message: '请输入需求数量', trigger: 'blur' }
        ],
        unit: [
          { required: true, message: '请输入单位', trigger: 'blur' }
        ],
        expectedDate: [
          { required: true, message: '请选择期望收到日期', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    const demandId = this.$route.params.id
    this.getCategories()
    this.getDemand(demandId)
  },
  methods: {
    getCategories() {
      listCategory().then(response => {
        this.categoryOptions = response.data
      })
    },
    getDemand(demandId) {
      getDemand(demandId).then(response => {
        this.form = response.data
      })
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          updateDemand(this.form).then(response => {
            this.$modal.msgSuccess('修改成功')
            this.cancel()
          })
        }
      })
    },
    cancel() {
      this.$router.push({ path: '/aid/demand' })
    }
  }
}
</script> 